<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI API 测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .input-group {
            margin-bottom: 20px;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 4px;
            border-left: 4px solid #007bff;
        }
        .error {
            border-left-color: #dc3545;
            background: #f8d7da;
        }
        pre {
            background: #f1f1f1;
            padding: 10px;
            border-radius: 4px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI API 测试页面</h1>
        <p>这个页面用于测试AI接口是否正常工作</p>
        
        <div class="input-group">
            <label for="message">输入消息：</label>
            <input type="text" id="message" value="你好，请介绍一下你自己" placeholder="请输入要发送给AI的消息">
        </div>
        
        <button onclick="testAI()">测试AI接口</button>
        
        <div id="result" class="result" style="display: none;">
            <h3>测试结果：</h3>
            <div id="resultContent"></div>
        </div>
    </div>

    <script>
        async function testAI() {
            const message = document.getElementById('message').value;
            const resultDiv = document.getElementById('result');
            const resultContent = document.getElementById('resultContent');
            
            if (!message.trim()) {
                alert('请输入消息内容');
                return;
            }
            
            resultDiv.style.display = 'block';
            resultContent.innerHTML = '<p>正在测试...</p>';
            
            try {
                console.log('开始测试AI接口...');
                console.log('发送消息:', message);
                
                const response = await fetch('http://localhost:8083/XinAi/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        message: message
                    })
                });
                
                console.log('响应状态:', response.status);
                console.log('响应头:', response.headers);
                
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }
                
                const data = await response.json();
                console.log('响应数据:', data);
                
                // 显示结果
                let html = `
                    <h4>✅ 请求成功</h4>
                    <p><strong>状态码:</strong> ${response.status}</p>
                    <p><strong>响应时间:</strong> ${data.duration || 'N/A'}ms</p>
                    <p><strong>成功状态:</strong> ${data.success}</p>
                    <p><strong>消息:</strong> ${data.message}</p>
                    <p><strong>用户消息:</strong> ${data.userMessage}</p>
                    <p><strong>AI回复:</strong> ${data.aiResponse || '无'}</p>
                    <hr>
                    <h4>完整响应数据:</h4>
                    <pre>${JSON.stringify(data, null, 2)}</pre>
                `;
                
                resultContent.innerHTML = html;
                resultDiv.className = 'result';
                
            } catch (error) {
                console.error('测试失败:', error);
                
                let html = `
                    <h4>❌ 请求失败</h4>
                    <p><strong>错误信息:</strong> ${error.message}</p>
                    <hr>
                    <h4>可能的原因：</h4>
                    <ul>
                        <li>后端服务未启动</li>
                        <li>端口8083不可访问</li>
                        <li>CORS跨域问题</li>
                        <li>网络连接问题</li>
                    </ul>
                    <hr>
                    <h4>调试信息:</h4>
                    <pre>${error.stack || error.message}</pre>
                `;
                
                resultContent.innerHTML = html;
                resultDiv.className = 'result error';
            }
        }
        
        // 页面加载完成后自动测试
        window.onload = function() {
            console.log('页面加载完成，可以开始测试');
        };
    </script>
</body>
</html> 